import {Page, Pageable} from "../utils/Page";
import fetcher from "./Fetcher";
import {PreviewMonthlySalaryDocumentsResponse} from "./MarketShoppingGuideSalaryDocumentApi";
import {NumberIdAndName} from "../types/BasicModels";
import {InnerGoods} from "./MarketShoppingGuideSalaryPlanResultApi";

export function findMonthlyMarketSettlementSummary(marketId:number,pageable:Pageable){
    return fetcher.get<Page<MarketSettlementPlanResultSummary>>(`markets/${marketId}/settlementPlans/results`,{
        ...pageable.toRequestParams()
    })
}

export interface MarketSettlementPlanResultSummary{
    year:number
    month:number
    totalFee:number
}

export function previewMonthlySettlementDocuments(marketId:number,year:number,month:number){
    return fetcher.get<PreviewMonthlySettlementResultsResponse>(`markets/${marketId}/settlementPlans/results/year/${year}/month/${month}/previewMonthly`)
}

/**
 * 应结算金额=总销量-总费用
 */
export interface PreviewMonthlySettlementResultsResponse {
    /**
     * 总费用
     */
    totalFee: number;
    /**
     * 总销量
     */
    totalSale: number;
    ruleFeeList: PreviewMonthlySettlementResultsRuleFeeResponse[];
}

export interface PreviewMonthlySettlementResultsRuleFeeResponse {
    id: number;
    name: string;
    note?: string;
    /**
     * 费用计算结果
     */
    fee: number;
}


export function findMonthlySettlementDocuments(marketId:number,year:number,month:number){
    return fetcher.get<MarketSettlementPlanResultResponse>(`markets/${marketId}/settlementPlans/results/year/${year}/month/${month}`)
}

export interface MarketSettlementPlanResultResponse{
    /**
     * 总销量
     */
    totalSale:number
    feeList:PreviewMonthlySettlementResultsRuleFeeResponse[]
}

export function generateMarketSettlementResult(marketId:number,year:number,month:number){
    return fetcher.post(`markets/${marketId}/settlementPlans/results/year/${year}/month/${month}/generateMonthly`)
}

/**
 * 获取已复制的薪资方案结果
 * @param marketId 卖场ID
 * @param year
 * @param month
 */
export function getCopiedMarketSettlementPlanResultPlan(
    marketId: number,
    year:number,
    month:number
) {
    return fetcher.get<MarketSettlementPlanResultPlanResponse>(`markets/${marketId}/settlementPlans/results/year/${year}/month/${month}/plan`);
}


/**
 * 导购薪资规则数据
 */
export interface MarketSettlementPlanResultRuleData {
    name: string;
    includedBrands: NumberIdAndName[];
    includedCategories: NumberIdAndName[];
    includedBrandTags: NumberIdAndName[];
    includedGoodsList: InnerGoods[];
    commissionPercentage?: number; // BigDecimal 转换为 number
    note?: string;
    fixed?: number; // BigDecimal 转换为 number
}

/**
 * 导购薪资方案结果响应
 */
export interface MarketSettlementPlanResultPlanResponse {
    rules: MarketSettlementPlanResultRuleData[];
}
